﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Betop.Lib.Core.Base;
using Betop.Lib.Enums;
using Betop.Lib.Utilities;


namespace Betop.Lib.Core.Config
{
    public class SysConfigBO:CommonOperate<SysConfigInfo>
    {
        private SysConfigDAL _config = new SysConfigDAL();


        protected override BaseDAL DAL
        {
            get
            {
                return _config;
            }
        }

        public SysConfigBO()
            : base(TableName.SysConfig.ToString())
        {
        }



        public override void Save(SysConfigInfo info)
        {            
            base.Save(info,_config.IsInsert(info.ID));
        }


        public void Save<T>(T info, ConfigID id)
        {
            string str = Serialize.BinarySerialize(info);

            SysConfigInfo cfgInfo = base.GetInfo(id.ToString());

            cfgInfo = (cfgInfo == null) ? new SysConfigInfo() : cfgInfo;

            cfgInfo.ID = id.ToString();
            cfgInfo.Config = str;

            this.Save(cfgInfo);
        }

        public T GetInfo<T>(ConfigID id) where T:new()
        {
            SysConfigInfo cfgInfo = base.GetInfo(id.ToString());

            T info = new T();

            if (cfgInfo != null)
            {
                info = (T)Serialize.BinaryDeserialize(cfgInfo.Config);
            }

            return info;
            
        }

        //private static Dictionary<string, object> _configs = new Dictionary<string,object>();
      
        //private static DateTime _registerLastUpdateTime = DateTime.Now;

        //public static RegisterInfo Register
        //{
        //    get
        //    {
        //        RegisterInfo info = null;
        //        if (!_configs.ContainsKey(ConfigID.reginfo.ToString()) || _registerLastUpdateTime.AddMinutes(5) < DateTime.Now)
        //        {
        //            SysConfigBO config = new SysConfigBO();
        //            info = config.GetInfo<RegisterInfo>(ConfigID.reginfo);
        //            _configs[ConfigID.reginfo.ToString()] = info;

        //        }
        //        else
        //        {
        //            if (_configs.ContainsKey(ConfigID.reginfo.ToString()))
        //            {
        //                info = _configs[ConfigID.reginfo.ToString()] as RegisterInfo;
        //            }
        //        }
        //        return info;
        //    }
        //}

       

       
    }
}
